﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace FileSystemLinqProviderConsoleApp
{
    internal class ExpressionTreeModifier : ExpressionVisitor
    {
        private IQueryable<FileSystemElement> fileSystemElements;

        internal ExpressionTreeModifier(IQueryable<FileSystemElement> fileSystemElements)
        {
            this.fileSystemElements = fileSystemElements;
        }

        internal Expression CopyAndModify(Expression expression)
        {
            return Visit(expression);
        }

        protected override Expression VisitConstant(ConstantExpression c)
        {
            // Replace the constant FileSystemContext arg with the queryable fileSystemElements.
            if (c.Type == typeof(FileSystemContext))
            {
                return Expression.Constant(fileSystemElements);
            }
            else
            {
                return c;
            }
        }
    }
}
